<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // prmise中的then 里面的函数如果执行 指的是它上一个异步调用已经调用了resolve 才会执行then里面的方法
      // then里面return一个Promise 相当于上面的异步执行完resolve 在当前的then里打印完上一步的执行结果 开始执行当前then中return出去的方法 然后它的结果会在下一个then中接收
      let P = [];
      let promise = Promise.resolve("参数");
      let fn = function (data) {
        console.log("同步1" + data);
      };
      let asyncFn = function (data) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            console.log("异步2" + data);
            resolve(data);
          }, 3000);
        });
      };
      let asyncFn2 = function (data) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            console.log("异步2" + data);
            resolve(data);
          }, 3000);
        });
      };
      let fn2 = function (data) {
        console.log("同步2" + data);
        return data;
      };
      let chain = [];
      chain = [fn, asyncFn, asyncFn2, fn2];

      for (let i = 0; i < chain.length; i++) {
        let fn = chain[i];
        promise = promise.then(fn);
      }
    </script>
  </body>
</html>
